using System;
using System.Collections.Generic;
using Gj.Steam;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;

namespace Gj
{
    [System.Serializable]
    public struct ChannelIdentity
    {
        public string channel;
        public string id;
    }
    [System.Serializable]
    public struct ChannelInfo
    {
        public string channel;
        public string id;
        public string suffix;
        public string prefix;
        public string keyAlias;
        public string keyPassword;
        public string appName;
#if UNITY_EDITOR
        public BuildTarget target;
#endif
        public bool needReal;
        public bool isChina;
        public bool single;
        public bool debug;
        public bool needPrivacy;
        public bool needPrivacyDialog;
        public List<SocialManager.Platform> social;
        public List<AdManager.Platform> ad;
        public List<AnalyticsManager.AnalyticsProvider> analytics;
    }
    public class ChannelManager
    {
        public const string IOS = "Ios";
        public const string Mac = "Mac";
        public const string Win = "Win";

        public static ChannelSettings settings;
        public static bool Inited { get; private set; }
        public static Dictionary<string, ChannelInfo> channelMap = new Dictionary<string, ChannelInfo>();

        private static ChannelInfo info = new ChannelInfo(){social = new List<SocialManager.Platform>()};
        static ChannelManager()
        {
            settings = LoadSettings();
            for (var i = 0; i < settings.channels.Count; i++)
            {
                var info = settings.channels[i];
                // 根据id唯一，支持不同平台的相同channel
                channelMap[info.id] = info;
            }
        }

        public static void Init()
        {
            GetChannel((c, i) =>
            {
                LogTools.Log("channel init:"+c+":"+i);
                if (channelMap.TryGetValue(i, out info))
                {
                    if (info.channel != c) info.channel = c;
                }
                else
                {
                    // todo 配置测试环境配置
                    info.isChina = false;
                    info.needReal = false;
                    // info.social = new List<SocialManager.Platform>() { SocialManager.Platform.TapCn };
                    info.social = new List<SocialManager.Platform>() {  };
                    info.analytics = new List<AnalyticsManager.AnalyticsProvider>();
                    info.ad = new List<AdManager.Platform>();
                }

                Inited = true;
                LogTools.Log("channel inited:"+info.channel+":"+info.id);
                LogTools.Log("channel info:"+info.social);
            });
        }

        private static ChannelSettings LoadSettings()
        {
            ChannelSettings loadedSettings = (ChannelSettings)Resources.Load("ChannelSettings", typeof(ChannelSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<ChannelSettings>();
                LogTools.Log("ChannelSettings is not exist (Resources)");
            }
            return loadedSettings;
        }

        public static string Channel()
        {
            return info.channel;
        }

        public static string ChannelId()
        {
            return info.id;
        }

        public static ChannelInfo Info()
        {
            return info;
        }

        private static ChannelIdentity ChannelFile()
        {
            return settings.Default;
        }

        private static void GetChannel(ChannelDelegate callback)
        {
#if UNITY_ANDROID && !UNITY_EDITOR
            try
            {
                Run(delegate
                {
                    var androidChannelListener = new AndroidChannelListener(callback);
                    ChannelSDK.CallStatic("GetChannel", androidChannelListener);
                });
            }
            catch (AndroidJavaException exp)
            {
                Debug.LogError(exp.Message);
            }

#elif UNITY_IOS && !UNITY_EDITOR
            callback(IOS, "");
#elif UNITY_STANDALONE_OSX && !UNITY_EDITOR
            var c = ChannelFile();
            if (c.id != ""){
                callback(c.channel, c.id);
            }else if(Steam.Steam.In){
                // 遍历包含steam的渠道信息
                callback("Steam", "steam");
            }else{
                callback(Mac, "");
            }
#elif UNITY_STANDALONE_WIN && !UNITY_EDITOR
            var c = ChannelFile();
            if (c.id != ""){
                callback(c.channel, c.id);
            }else if(Steam.Steam.In){
                callback("Steam", "steam");
            }else{
                callback(Win, "");
            }
#elif UNITY_STANDALONE
            var c = ChannelFile();
            if (c.id != ""){
                callback(c.channel, c.id);
            }else if(Steam.Steam.In){
                callback("Steam", "steam");
            }else{
                callback("unknow", "");
            }
#else 
            callback("unknow", "unknow");
#endif
        }
        // 成就回调
        public delegate void ChannelDelegate(string channel, string id);

#if UNITY_ANDROID && !UNITY_EDITOR

        public delegate void Action();

        static void Run(Action action)
        {
            activity.Call("runOnUiThread", new AndroidJavaRunnable(action));
        }

        class AndroidChannelListener : AndroidJavaProxy
        {
            ChannelDelegate channelDelegate = null;

            public AndroidChannelListener(ChannelDelegate Delegate)
                : base("com.ueffort.channel.ChannelListener")
            {
                this.channelDelegate = Delegate;
            }

            public void onResult(string channel, string id)
            {
                channelDelegate(channel, id);
            }
        }

        static AndroidJavaClass ChannelSDK = new AndroidJavaClass("com.ueffort.channel.ChannelSDK");

        static AndroidJavaObject activity =
            new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");

#endif
    }
}